package com.mojawa.mj_util.time

import android.os.Handler
import android.os.Looper
import android.util.Log

/**
 * 计时管理器 -- 可封装在base模块
 */
class MJTimeManager private constructor() {

    private var startTime: Long = 0L
    private var pauseTime: Long = 0L
    private var handler: Handler = Handler(Looper.getMainLooper())
    private var runnable: Runnable? = null
    private var listener: TimerUpdateListener? = null
    private var isRunning = false
    private var isPaused = false

    interface TimerUpdateListener {
        fun onTimerUpdate(seconds: Long)
    }

    companion object {
        @Volatile
        private var instance: MJTimeManager? = null

        fun getInstance(): MJTimeManager {
            return instance ?: synchronized(this) {
                instance ?: MJTimeManager().also { instance = it }
            }
        }
    }

    // 启动计时器
    fun startTimer(listener: TimerUpdateListener) {
        if (!isRunning && !isPaused) {
            this.listener = listener
            startTime = System.currentTimeMillis() // 记录开始时间
            isRunning = true
            runnable = object : Runnable {
                override fun run() {
                    val elapsedTime = (System.currentTimeMillis() - startTime) / 1000
                    listener.onTimerUpdate(elapsedTime)
                    handler.postDelayed(this, 1000)
                }
            }
            handler.post(runnable!!)
        } else if (isPaused) {
            resumeTimer(listener)
        } else {
            // 如果计时器正在运行，直接更新监听器
            this.listener = listener
            listener.onTimerUpdate((System.currentTimeMillis() - startTime) / 1000)
        }
    }

    // 暂停计时器
    fun pauseTimer() {
        if (isRunning) {
            runnable?.let { handler.removeCallbacks(it) }
            pauseTime = System.currentTimeMillis() - startTime // 记录暂停时的总时间
            isRunning = false
            isPaused = true
            Log.i("TimerManager", "Timer paused at ${pauseTime / 1000} seconds")
        }
    }

    // 恢复计时器
    fun resumeTimer(listener: TimerUpdateListener) {
        if (isPaused) {
            this.listener = listener
            startTime = System.currentTimeMillis() - pauseTime // 调整开始时间以保持一致
            isRunning = true
            isPaused = false
            Log.i("TimerManager", "Timer resumed at ${pauseTime / 1000} seconds")
            runnable?.let { handler.post(it) }
        }
    }

    // 停止并销毁计时器
    fun stopAndDestroyTimer() {
        stopTimer()
        instance = null
    }

    // 停止计时器
    fun stopTimer() {
        runnable?.let { handler.removeCallbacks(it) }
        runnable = null
        isRunning = false
        isPaused = false
        Log.i("TimerManager", "Timer stopped")
    }

    // 重置计时器
    fun resetTimer() {
        stopTimer()
        startTime = 0L
        pauseTime = 0L
        Log.i("TimerManager", "Timer reset")
    }

    // 获取当前计时的秒数
    fun getSeconds(): Long {
        return if (isRunning) {
            (System.currentTimeMillis() - startTime) / 1000
        } else {
            pauseTime / 1000
        }
    }
}

